package cn.huolabao.common.swagger;

import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
public class SwaggerConfig implements WebMvcConfigurer {

    @Bean
    public Docket docket() {
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(appinfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("cn.huolabao.controller"))
                .paths(PathSelectors.any())
                .build();
        docket.securitySchemes(securitySchemes())
                .securityContexts(securityContexts());
        return docket;
    }

    private List<SecurityContext> securityContexts() {

        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessResource");
        AuthorizationScope[] authorizationScopes = {authorizationScope};
        SecurityReference securityReference = new SecurityReference("Authorization", authorizationScopes);
        final List<SecurityReference> securityReferences = Collections.singletonList(securityReference);
        return Collections.singletonList(new SecurityContext(securityReferences, PathSelectors.any()));
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    private List<? extends SecurityScheme> securitySchemes() {
        return Collections.singletonList(new ApiKey("Token", "Authorization", "Authorization"));
    }

    private ApiInfo appinfo() {
        ApiInfoBuilder apiInfoBuilder = new ApiInfoBuilder();
        Contact contact = new Contact("leechee-cloud", "https://www.huolabao.cn/", "zmissu@163.com");
        apiInfoBuilder.contact(contact);
        apiInfoBuilder.title("leechee-cloud");
        apiInfoBuilder.description("leechee-cloud 分布式微服务");
        apiInfoBuilder.version("1.0");
        return apiInfoBuilder.build();
    }
}
